﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace EpcTagLibrary.Pat
{
    public class SGLN : Pattern
    {
        private int m_BitLength;

        public SGLN(PatternField[] fields, Type type)
            : base(fields, type)
        {
        }

        public override int GetTagBitLength()
        {
            return m_BitLength;
        }

        public override string GetScheme()
        {
            return "sgln-96";
        }

        public override string GetSchemeName()
        {
            return "sgln";
        }

        public PatternField FilterComponent
        {
            get
            {
                return Fields[0];
            }
        }

        public PatternField CompanyPrefixComponent
        {
            get
            {
                return Fields[1];
            }
        }

        public PatternField LocationReferenceComponent
        {
            get
            {
                return Fields[2];
            }
        }

        public PatternField ExtensionComponent
        {
            get
            {
                return Fields[3];
            }
        }

        public class Factory : PatternFactory
        {
            public override Pattern GetPattern(string patBody, bool isGroupingPattern, bool isPureid)
            {
                if (!patBody.StartsWith("sgln"))
                {
                    throw new PatternParseException("Specified URI does not match the sgln URI for patterns.");
                }

                int Length = 195;

                if (!isPureid)
                {
                    Length = System.Convert.ToInt32(patBody.Substring(5, patBody.IndexOf(":")-5));
                }

                string[] Fields = patBody.Substring(patBody.IndexOf(":") + 1).Split(new char[1] { '.' });

                if (Fields.Length != 4)
                {
                    throw new PatternParseException("Specified URI does not match the SGLN URI for patterns.");
                }

                PatternField[] PatFields = new PatternField[4];
                try
                {
                    PatFields[0] = new PatternField(Fields[0], isGroupingPattern, 3);
                    PatFields[0].Property = typeof(EpcTagLibrary.SGLN).GetProperty("Filter");

                    PatFields[1] = new PaddedPatternField(Fields[1], isGroupingPattern, 40);
                    PatFields[1].Property = typeof(EpcTagLibrary.SGLN).GetProperty("CompanyPrefix");

                    PatFields[2] = new PaddedPatternField(Fields[2], isGroupingPattern, 21);
                    PatFields[2].Property = typeof(EpcTagLibrary.SGLN).GetProperty("LocationReference");

                    PatFields[3] = new PaddedPatternField(Fields[3], isGroupingPattern, Length == 96 ? 41 : 140);
                    PatFields[3].Property = typeof(EpcTagLibrary.SGLN).GetProperty("ExtensionComponent");
                }
                catch (Exception e)
                {
                    throw new PatternParseException("Error occured setting up pattern component for a field: " + patBody, e);
                }

                SGLN Result = new SGLN(PatFields, typeof(EpcTagLibrary.SGLN));
                Result.m_BitLength = Length;

                return Result;
            }
        }
    }
}
